﻿using Pipes.Xml;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.IO;

namespace Pipes.UnitTest
{
    
    
    /// <summary>
    ///This is a test class for XmlTextWriterTest and is intended
    ///to contain all XmlTextWriterTest Unit Tests
    ///</summary>
  [TestClass()]
  public class XmlTextWriterTest
  {


    private TestContext testContextInstance;

    /// <summary>
    ///Gets or sets the test context which provides
    ///information about and functionality for the current test run.
    ///</summary>
    public TestContext TestContext
    {
      get
      {
        return testContextInstance;
      }
      set
      {
        testContextInstance = value;
      }
    }

    #region Additional test attributes
    // 
    //You can use the following additional attributes as you write your tests:
    //
    //Use ClassInitialize to run code before running the first test in the class
    //[ClassInitialize()]
    //public static void MyClassInitialize(TestContext testContext)
    //{
    //}
    //
    //Use ClassCleanup to run code after all tests in a class have run
    //[ClassCleanup()]
    //public static void MyClassCleanup()
    //{
    //}
    //
    //Use TestInitialize to run code before running each test
    //[TestInitialize()]
    //public void MyTestInitialize()
    //{
    //}
    //
    //Use TestCleanup to run code after each test has run
    //[TestCleanup()]
    //public void MyTestCleanup()
    //{
    //}
    //
    #endregion


    /// <summary>
    /// XMLs the read write.
    /// </summary>
    [TestMethod()]
    public void XmlReadWrite()
    {
      var expected = "<div><p>something</p>\r\n   <!-- a comment -->  <div id=\"something\" style=\"display:block;\"><p>another \r\nvalue</p>\r\n<p>finally</p></div></div>";
      var reader = new Pipes.IO.StringTextSource(expected).Pipe(new Pipes.Xml.XmlTextReader());
      reader.WhitespaceHandling = System.Xml.WhitespaceHandling.All;
      var writer = new System.IO.StringWriter();
      var settings = new Pipes.Xml.XmlWriterSettings();
      settings.OmitXmlDeclaration = true;
      var xml = Pipes.Xml.XmlTextWriter.Create(writer, settings);
      foreach (var item in reader)
      {
        xml.Node(item);
      }
      xml.Flush();
      Assert.AreEqual(expected, writer.ToString());
    }

    /// <summary>
    /// XMLs the read write.
    /// </summary>
    [TestMethod()]
    public void HtmlReadWrite()
    {
      var expected = "<!DOCTYPE html> <div><p>something</p>\r\n   <!-- a comment -->  <div id=\"something\" style=\"display:block;\"><p>another \r\nvalue</p>\r\n<p>finally</p></div></div>";
      var reader = new Pipes.IO.StringTextSource(expected).Pipe(new Pipes.Sgml.SgmlTextReader());
      var writer = new System.IO.StringWriter();
      var xml = Pipes.Sgml.HtmlTextWriter.Create(writer);
      foreach (var item in reader)
      {
        xml.Node(item);
      }
      xml.Flush();
      Assert.AreEqual(expected, writer.ToString());
    }
  }
}
